package com.example.activitylifecycle;

import org.apkplug.Bundle.BundleActivityLifecycleCallbacks;
import org.apkplug.Bundle.RegActivityLifecycleCallbacks;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;

import android.app.Activity;
import android.os.Bundle;
public class SimpleBundle implements BundleActivator
{
	private ServiceRegistration m_reg = null;
	private BundleActivityLifecycleCallbacksImp imp=null;
    public void start(BundleContext context) throws Exception
    {
        System.out.println("Simple Bundle " + context.getBundle().getBundleId()
            + " has started.");
        imp=new BundleActivityLifecycleCallbacksImp();
        ServiceReference ref  =  context.getServiceReference(RegActivityLifecycleCallbacks.class.getName());
        if  (ref  !=   null ) {
        	//查找到服务
        	RegActivityLifecycleCallbacks service  =  (RegActivityLifecycleCallbacks) context.getService(ref);
        	 if  (service  !=   null ) {
        		//将BundleActivityLifecycleCallbacksImp activity状态监听器注册到ActivityLifecycle管理器中
        		 service.registerActivityLifecycleCallbacks(imp);
        		 context.ungetService(ref);
        	 }
        }
    }
   
    public void stop(BundleContext context)
    {
        System.out.println("Simple Bundle " + context.getBundle().getBundleId()
            + " has stopped.");
        ServiceReference ref  =  context.getServiceReference(RegActivityLifecycleCallbacks.class.getName());
        if  (ref  !=   null ) {
        	
        	RegActivityLifecycleCallbacks service  =  (RegActivityLifecycleCallbacks) context.getService(ref);
        	 if  (service  !=   null ) {
        		
        		 service.unregisterActivityLifecycleCallbacks(imp);
        		
        		 context.ungetService(ref);
        	 }
        }
    }
}
